#include "stdafx.h"
#include "ui_patternbox.h"

#include <qpixmap.h>
#include <qstringlist.h>

#include "pf_debug.h"


/**
 * Default Constructor. You must call init manually if you choose
 * to use this constructor.
 */
UI_PatternBox::UI_PatternBox(QWidget* parent, const char* name)
        : QComboBox(parent, name) {
}



/**
 * Destructor
 */
UI_PatternBox::~UI_PatternBox() {}


/**
 * Initialisation (called manually and only once).
 */
void UI_PatternBox::init() {
    QStringList patterns;

    QListIterator<PF_Pattern *> f = PF_PATTERNLIST->getIteretor();
    while (f.hasNext()) {
        patterns.append( f.next()->getFileName() );
    }

    patterns.sort();
    insertStringList(patterns);

    connect(this, SIGNAL(activated(int)),
            this, SLOT(slotPatternChanged(int)));

    setCurrentItem(0);
    slotPatternChanged(currentItem());
}



/**
 * Sets the currently selected width item to the given width.
 */
void UI_PatternBox::setPattern(const QString& pName) {

    PF_DEBUG->print("UI_PatternBox::setPattern %s\n", pName.toLatin1().data());

    setCurrentText(pName);

    slotPatternChanged(currentItem());
}



/**
 * Called when the pattern has changed. This method 
 * sets the current pattern to the value chosen.
 */
void UI_PatternBox::slotPatternChanged(int index) {

    PF_DEBUG->print("UI_PatternBox::slotPatternChanged %d\n", index);

    currentPattern = PF_PATTERNLIST->requestPattern(currentText());

    if (currentPattern!=NULL) {
        PF_DEBUG->print("Current pattern is (%d): %s\n",
                        index, currentPattern->getFileName().toLatin1().data());
    }

    emit patternChanged(currentPattern);
}


